import time

from selenium import webdriver

# 打开浏览器
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://mail.163.com/')
driver.maximize_window()

#通过标签下标获取iframe
iframe = driver.find_elements(By.TAG_NAME, 'iframe')[0]

#切换到获取的frame
driver.switch_to.frame(iframe)

#获取账号框并输入
time.sleep(1)
driver.find_element(By.NAME, 'email').send_keys('WU942981954')

#获取密码框并输入
time.sleep(1)
driver.find_element(By.NAME, 'password').send_keys('518613wu!')

#获取登录按钮并登录
time.sleep(2)
driver.find_element(By.ID, 'dologin').click()

# 退出frame
driver.switch_to.default_content()

# 定位到写信功能
time.sleep(2)
driver.find_element(By.ID, '_mail_component_149_149').click()

# 收件人框输入邮箱
time.sleep(2)
driver.find_element(By.CLASS_NAME,'nui-editableAddr-ipt').send_keys('周星驰')

# 定位到主题输入内容
time.sleep(2)
driver.find_element(By.XPATH, '//header/div[2]/div[1]/div/div/input').send_keys('喜剧之王')

# 切换到内容框frame
time.sleep(2)
iframe = driver.find_elements(By.TAG_NAME, 'iframe')[3]
driver.switch_to.frame(iframe)

# 在内容框输入内容
time.sleep(2)
driver.find_element(By.CSS_SELECTOR, 'body').send_keys('无厘头喜剧')

# 回到主页面
time.sleep(2)
driver.switch_to.default_content()

# 定位发送按钮并点击
time.sleep(2)
driver.find_elements(By.CLASS_NAME, 'nui-btn-icon')[2].click()

# 退出浏览器
time.sleep(2)
# driver.quit()